package com.kh.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kh.reggie.common.CustomerException;
import com.kh.reggie.common.Result;
import com.kh.reggie.dto.SetmealDto;
import com.kh.reggie.entity.Category;
import com.kh.reggie.entity.Dish;
import com.kh.reggie.entity.Setmeal;
import com.kh.reggie.entity.SetmealDish;
import com.kh.reggie.service.CategoryService;
import com.kh.reggie.service.SetmealDishService;
import com.kh.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        LambdaQueryWrapper<Object> queryWrapper = new LambdaQueryWrapper<>();
        setmealService.saveWithDish(setmealDto);
        return Result.success("新增套餐成功");
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Page<SetmealDto> page2 = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1,queryWrapper);
        //单纯拷贝分页信息，几条数据几页
        BeanUtils.copyProperties(page1,page2,"records");
        List<Setmeal> records = page1.getRecords();

        List<SetmealDto> list =records.stream().map(i->{
            SetmealDto setmealDto = new SetmealDto();
            //拷贝records
            BeanUtils.copyProperties(i,setmealDto);
            //分类ID
            Long categoryId = i.getCategoryId();
            //根据ID查分类对象
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());


        page2.setRecords(list);
        return Result.success(page2);
    }

    /**
     * 批量/单个删除套餐(逻辑删除)
     *
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐几个:{}",ids.size());
        //真正删除
//        setmealService.removeWithDish(ids);
        if (ids == null || ids.isEmpty()) {
            return Result.error("传入的ID列表为空");
        }
        //判断是否在售，在Server层操作简单点，controller要写Mapper,这里不写了


        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids).set(Setmeal::getIsDeleted,1);
        // 执行批量更新操作
        boolean updated = setmealService.update(null, updateWrapper);

        LambdaUpdateWrapper<SetmealDish> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.in(SetmealDish::getSetmealId,ids).set(SetmealDish::getIsDeleted,1);
        // 执行批量更新操作
        boolean updated1 = setmealDishService.update(null, updateWrapper1);

        if (updated && updated1) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }

    }

    /**
     * 起售几个套餐
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public Result<String> statusChange1(@RequestParam  List<Long> ids ){
        log.info("起售几个套餐:{}",ids);
        if (ids == null || ids.isEmpty()) {
            return Result.error("传入的ID列表为空");
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids).set(Setmeal::getStatus,1);

        boolean updated = setmealService.update(null,updateWrapper);

        if (updated) {
            return Result.success("起售成功");
        } else {
            return Result.error("起售失败");
        }
    }

    /**
     * 停售几个套餐
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public Result<String> statusChange0(@RequestParam List<Long> ids ){
        log.info("停售几个套餐:{}",ids.size());
        if (ids == null || ids.isEmpty()) {
            return Result.error("传入的ID列表为空");
        }
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids).set(Setmeal::getStatus,0);

        boolean updated = setmealService.update(null,updateWrapper);

        if (updated) {
            return Result.success("停售成功");
        } else {
            return Result.error("停售失败");
        }
    }
}
